<?php

namespace App\Http\Controllers\Api;

use App\Models\UserAddress;
use Illuminate\Http\Request;

class UserAddressesController extends ApiController
{
    /**
     * $app = app('wechat.mini_program');
     * @return mixed
     */
    public function index()
    {
        //用户登录则记录
        if (auth('api')->user()) {
            $addresses = UserAddress::where(['user_id' => auth('api')->user()->id])->orderBy('is_default','desc')->get();
            $data['addresses'] = $addresses;
            return $this->success($data);
        }
        return $this->success('请登录', 'error');
    }
    public function addresById(Request $request)
    {
        $address = UserAddress::find($request->id);
        if (auth('api')->user()->id == $address->user->id) {
            $data['address'] = $address;
            return $this->success($data);
        } else {
            return $this->success('地址归属错误', 'error');
        }
    }
    public function store(Request $request)
    {
        if ($request->id) {
            $data = [
                'user_id' => auth('api')->user()->id,
                'province' => $request->province,
                'city' => $request->city,
                'district' => $request->district,
                'address' => $request->address,
                'contact_name' => $request->contact_name,
                'contact_phone' => $request->contact_phone,
                'is_default' => $request->is_default,
            ];
            $res = $this->update($request->id,$data);
            if($res)
            {
                return $this->success('');
            }
            else{
                return $this->success('保存失败','error');
            }
        } else {
            //用户登录则记录
            if (auth('api')->user()) {
                try {
                    $addresses = UserAddress::create([
                        'user_id' => auth('api')->user()->id,
                        'province' => $request->province,
                        'city' => $request->city,
                        'district' => $request->district,
                        'address' => $request->address,
                        'contact_name' => $request->contact_name,
                        'contact_phone' => $request->contact_phone,
                        'is_default' => $request->is_default,
                    ]);
                    if ($request->is_default) {
                        $this->default($addresses->id);
                    }
                    $data['addresses'] = $addresses;
                    return $this->success($data);
                } catch (\Exception $e) {
                    return $this->success('保存失败', 'error');
                }
            }
            return $this->success('请登录', 'error');
        }
    }
    public function update($id,$data)
    {
        //用户登录则记录
        if (auth('api')->user()) {
            try {
                $addresses = UserAddress::where('id', $id)->update([
                    'province' => $data['province'],
                    'city' => $data['city'],
                    'district' => $data['district'],
                    'address' => $data['address'],
                    'contact_name' => $data['contact_name'],
                    'contact_phone' => $data['contact_phone'],
                    'is_default' => $data['is_default'],
                ]);
                if ($data['is_default']) {
                    $this->default($id);
                }
                $data['addresses'] = $addresses;
                return true;
                return $this->success($data);
            } catch (\Exception $e) {
                return false;
                return $this->success('保存失败', 'error');
            }
        }
        return $this->success('请登录', 'error');
    }
    public function setDefault(Request $request)
    {
        $address = UserAddress::find($request->id);
        if (auth('api')->user()->id == $address->user->id) {
            $this->default($request->id);
            return $this->success('');
        } else {
            return $this->success('地址归属错误', 'error');
        }
    }
    public function destroy($id)
    {
        $address = UserAddress::find($id);
        if (auth('api')->user()->id == $address->user->id) {
            $address->delete();
            return $this->success('');
        } else {
            return $this->success('地址归属错误', 'error');
        }
    }

    function default($id) {
        UserAddress::where('user_id', auth('api')->user()->id)
            ->update(['is_default' => 0]);
        $address = UserAddress::find($id);
        $address->is_default = 1;
        $address->save();
    }
}
